const { By, Key } = require('selenium-webdriver')
const globalConfig = require('./config')

module.exports = function auth (driver) {
  if (driver.hasLogin) return
  return driver.get('https://www.qimai.cn/account/signin/r/%2F')
  .then(() => {
    driver.findElement(By.css('[name="username"]')).sendKeys(globalConfig.config.username)
    driver.findElement(By.css('[name="password"]')).sendKeys(globalConfig.config.password, Key.RETURN)
    return
  })
  .then(() => {
    driver.hasLogin = true
    return driver.sleep(2000)
  })
}